package main

import (
	"os"
	"strings"

	"gitee.com/love_bass/go-winsvc/winsvc"
)

var service winsvc.Service

func main() {
	svcConfig := &winsvc.Config{
		ServiceName:        "AAAAAA测试服务",
		ServiceDisplayName: "AAAAAA测试服务",
		ServiceDescription: "AAAAAA测试服务",
		StartOn:            startOn,
		StopOn:             stopOn,
	}
	service = winsvc.NewService(svcConfig)

	if len(os.Args) > 1 {
		handleServiceCommand(os.Args[1])
		return
	}

	err := service.RunApplication()
	if err != nil {
		panic(err)
	}
}

func startOn() {
	// 这里可以添加应用程序的启动逻辑
	// 例如：启动HTTP服务器、初始化数据库连接等
	winsvc.GetLogger().Info("应用程序启动完成")
}

func stopOn() {
	// 这里可以添加应用程序的清理逻辑
	// 例如：关闭HTTP服务器、断开数据库连接等
	winsvc.GetLogger().Info("应用程序清理完成")
}

// handleServiceCommand 处理服务控制命令
func handleServiceCommand(cmd string) {
	cmd = strings.ToLower(cmd)

	switch cmd {
	case "install":
		service.Install()

	case "uninstall":
		service.Uninstall()

	case "start":
		service.Start()

	case "stop":
		service.Stop()

	case "restart":
		service.Restart()

	default:
		service.MessageBox("无效命令", "不支持的命令: "+cmd+"\n\n支持的命令: install, uninstall, start, stop, restart", winsvc.MB_OK|winsvc.MB_ICONWARNING)
	}
}
